<?php

class Upload extends Zend\File\Transfer\Adapter\Http {

    public $remote_root_dir = "";
    public $remote_folder = "";
    public $local_path = "";
    public $local_file = "";

    function __construct($options = null){

        parent::__construct();

        if(isset($options['remote_folder']))
            $this->remote_folder = $options['remote_folder']; //"hall";
        if(isset($options['remote_root_dir']))
            $this->remote_root_dir = $options['remote_root_dir']; //"remontik.kh.ua";
        if(isset($options['local_path']))
            $this->local_path = $options['local_path'];
        if(isset($options['local_file']))
            $this->local_file = $options['local_file'];


    }

    function test($files){
        $check = $this->getFiles($files); fb($check);
        foreach ($check as $file => $content) {
            fb($content);
        }
    }

    function setFiles($files){
        $this->files = $files;
    }

    public function receive($files = null)
    {
        if (!$this->isValid($files)) {
            fb('Validation filed');
            $m = $this->getMessages();
            fb($m);
            return false;
        }

        $check = $this->getFiles($files);
        foreach ($check as $file => $content) {
            if (!$content['received']) {
                $directory   = '';
                $destination = $this->getDestination($file);
                if ($destination !== null) {
                    $directory = $destination . DIRECTORY_SEPARATOR;
                }

                $filename = $directory . $content['name'];
                $rename   = $this->getFilter('Rename');
                if ($rename !== null) {
                    $tmp = $rename->getNewName($content['tmp_name']);
                    if ($tmp != $content['tmp_name']) {
                        $filename = $tmp;
                    }

                    if (dirname($filename) == '.') {
                        $filename = $directory . $filename;
                    }

                    $key = array_search(get_class($rename), $this->files[$file]['filters']);
                    unset($this->files[$file]['filters'][$key]);
                }

                // Should never return false when it's tested by the upload validator
                if (!move_uploaded_file($content['tmp_name'], $filename)) {
                    if ($content['options']['ignoreNoFile']) {
                        $this->files[$file]['received'] = true;
                        $this->files[$file]['filtered'] = true;
                        continue;
                    }

                    $this->files[$file]['received'] = false;
                    return false;
                }

                if ($rename !== null) {
                    $this->files[$file]['destination'] = dirname($filename);
                    $this->files[$file]['name']        = basename($filename);
                }

                $this->files[$file]['tmp_name'] = $filename;
                $this->files[$file]['received'] = true;
            }

            if (!$content['filtered']) {
                if (!$this->filter($file)) {
                    $this->files[$file]['filtered'] = false;
                    return false;
                }

                $this->files[$file]['filtered'] = true;
            }

            // FTP UPLOAD
            if (true) {// ENAABLED
                /*
                $host = '83.142.230.166';
                $usr = 'ukfloor';
                $pwd = 'r7FnO74vGG';
                */



                $host = FTP_HOST;
                $usr = FTP_USER;
                $pwd = FTP_PASS;



                $uploadFilename = $filename;
                $uploadFilename = str_replace( $this->local_path, '', $filename);
                //$uploadFilename = str_replace(IMAGES_PATH, '', $filename);
                //$uploadFilename = str_replace('./public/images/', '', $uploadFilename);

                $localFilename = $filename;



                $ftpPath = 'public_html/'. $this->remote_root_dir .'/Image/' .  $this->remote_folder . "/" . $uploadFilename;
                $dir = 'public_html/'. $this->remote_root_dir .'/Image/' .  $this->remote_folder ;

                $ftpConnection = @ftp_connect($host);
                if ($ftpConnection) {

                    $ftpLogin = @ftp_login($ftpConnection, $usr, $pwd);
                    if ($ftpLogin) {

                        $is_dir = $this->ftpIsDir($ftpConnection,$dir);
                        if((bool)$is_dir === false){
                            ftp_mkdir($ftpConnection,$dir);
                        }

                        @ftp_put($ftpConnection, $ftpPath, $localFilename, FTP_BINARY);

                        $sm_file = PUBLIC_PATH . "/tmp/sm_" . $this->local_file  ; ///fb($sm_file);
                        if( file_exists($sm_file) ){
                            @ftp_put( $ftpConnection,
                                $ftpPath = 'public_html/'. $this->remote_root_dir .'/Image/' .  $this->remote_folder . "/" . 'sm_' . $this->local_file,
                                $localFilename = $sm_file,
                                FTP_BINARY
                            );
                        }
                    }
                    @ftp_close($ftpConnection);
                }

            }
            // END

        }

        return true;
    }

    private function ftpIsDir($ftp, $dir)    {
        // Get the current working directory
        $origin = ftp_pwd($ftp);

        // Attempt to change directory, suppress errors
        if (@ftp_chdir($ftp, $dir))
        {
            // If the directory exists, set back to origin
            ftp_chdir($ftp, $origin);
            return true;
        }

        // Directory does not exist
        return false;
    }

}